<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon.ico?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon.ico?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="JavaScript,">










<meta name="description" content="面向对象及原型1.1属性类型 ECMAScript中有两种属性：数据属性和访问器属性   数据属性  包含一个数据的位置，在这个位置可以进行读写，有4个描述行为的特性   Configurable表示能否通过delete删除属性从而重新定义属性，能否修改属性的特性，能否把属性修改为访问器属性  123456789101112131415161718192021//***************特性">
<meta name="keywords" content="JavaScript">
<meta property="og:type" content="article">
<meta property="og:title" content="面向对象和原型">
<meta property="og:url" content="http://zhanglei_c.gitee.io/blog/2019/10/18/面向对象和原型/index.html">
<meta property="og:site_name" content="爱吃零食的猫">
<meta property="og:description" content="面向对象及原型1.1属性类型 ECMAScript中有两种属性：数据属性和访问器属性   数据属性  包含一个数据的位置，在这个位置可以进行读写，有4个描述行为的特性   Configurable表示能否通过delete删除属性从而重新定义属性，能否修改属性的特性，能否把属性修改为访问器属性  123456789101112131415161718192021//***************特性">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://zhanglei_c.gitee.io/blog/imgs/1571382645865.png">
<meta property="og:updated_time" content="2019-10-25T12:10:47.796Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="面向对象和原型">
<meta name="twitter:description" content="面向对象及原型1.1属性类型 ECMAScript中有两种属性：数据属性和访问器属性   数据属性  包含一个数据的位置，在这个位置可以进行读写，有4个描述行为的特性   Configurable表示能否通过delete删除属性从而重新定义属性，能否修改属性的特性，能否把属性修改为访问器属性  123456789101112131415161718192021//***************特性">
<meta name="twitter:image" content="http://zhanglei_c.gitee.io/blog/imgs/1571382645865.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://zhanglei_c.gitee.io/blog/2019/10/18/面向对象和原型/">





  <title>面向对象和原型 | 爱吃零食的猫</title>
  








</head>

<script type="text/javascript" color="0,0,255" opacity="0.7" zindex="-2" count="99" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>
<a href="https://github.com/403NotFound" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewbox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"/><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"/><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"/></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">爱吃零食的猫</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">自由，平等，公正，法治</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://zhanglei_c.gitee.io/blog/blog/2019/10/18/面向对象和原型/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Zhang_lei">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="爱吃零食的猫">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">面向对象和原型</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-10-18T19:22:47+08:00">
                2019-10-18
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="面向对象及原型"><a href="#面向对象及原型" class="headerlink" title="面向对象及原型"></a>面向对象及原型</h2><h3 id="1-1属性类型"><a href="#1-1属性类型" class="headerlink" title="1.1属性类型"></a>1.1属性类型</h3><blockquote>
<p>ECMAScript中有两种属性：<strong>数据属性</strong>和<strong>访问器属性</strong></p>
</blockquote>
<ul>
<li><p>数据属性</p>
<blockquote>
<p>包含一个数据的位置，在这个位置可以进行读写，有4个描述行为的特性</p>
</blockquote>
<ul>
<li><strong><code>Configurable</code></strong>表示能否通过<code>delete</code>删除属性从而重新定义属性，能否修改属性的特性，能否把属性修改为访问器属性</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//***************特性1******************</span></span><br><span class="line"><span class="keyword">var</span> person = &#123;&#125;</span><br><span class="line"><span class="built_in">Object</span>.defineProperty(person,<span class="string">'name'</span>,&#123;</span><br><span class="line">    configurable: <span class="literal">false</span>,</span><br><span class="line">    value: <span class="string">'jack'</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="built_in">console</span>.log(person.name) <span class="comment">// "jack"</span></span><br><span class="line"><span class="keyword">delete</span> person.name <span class="comment">// 删除这个属性</span></span><br><span class="line"><span class="built_in">console</span>.log(person.name) <span class="comment">// "jack"</span></span><br><span class="line"><span class="comment">//***************特性2******************</span></span><br><span class="line"><span class="keyword">var</span> person = &#123;&#125;</span><br><span class="line"><span class="built_in">Object</span>.defineProperty(person,<span class="string">'name'</span>,&#123;</span><br><span class="line">    configurable: <span class="literal">false</span>,</span><br><span class="line">    value: <span class="string">'jack'</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// 报错</span></span><br><span class="line"><span class="comment">// 一旦把属性设置自成不可配置，就不能再把它变回可配置了</span></span><br><span class="line"><span class="built_in">Object</span>.defineProperty(person,<span class="string">'name'</span>,&#123;</span><br><span class="line">    configurable: <span class="literal">true</span>,</span><br><span class="line">    value: <span class="string">'jack'</span></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><strong><code>Enumerable</code></strong>表示能否通过for-in循环属性</li>
<li><p><strong><code>Writable</code></strong>表示能否修改属性的值</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> person = &#123;&#125;</span><br><span class="line"><span class="built_in">Object</span>.defineProperty(person,<span class="string">'name'</span>,&#123;</span><br><span class="line">    writable: <span class="literal">false</span>,</span><br><span class="line">    value: <span class="string">'jack'</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="built_in">console</span>.log(person.name) <span class="comment">// "jack"</span></span><br><span class="line">person.name = <span class="string">"bob"</span> <span class="comment">// 删除这个属性</span></span><br><span class="line"><span class="built_in">console</span>.log(person.name) <span class="comment">// "jack"</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong><code>Value</code></strong>包含这个属性的数据值</p>
</li>
</ul>
<ul>
<li><p>访问器属性</p>
<blockquote>
<p>不包含数据值，但是包含一对<code>getter</code>和<code>setter</code>函数，读取访问器属性时会调用<code>getter</code>,写入访问器属性时，会调用<code>setter</code></p>
</blockquote>
<ul>
<li><strong><code>Configurable</code></strong>和数据属性类似</li>
<li><strong><code>Eunmerable</code></strong>和数据属性类似</li>
<li><strong><code>Get</code></strong>在读取属性时调用,默认值是 undefined</li>
<li><strong><code>Set</code></strong>在写入属性时调用,默认值是 undefined</li>
</ul>
<p><strong>访问器属性不能直接定义，需要使用<code>Object.defineProperty()</code>来定义</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 插入一个 v-model 的数据双向绑定的原理 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">id</span>=<span class="string">"input"</span> <span class="attr">type</span>=<span class="string">"text"</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">span</span> <span class="attr">id</span>=<span class="string">"model-box"</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="undefined"></span></span><br><span class="line"><span class="javascript">	<span class="keyword">var</span> input = <span class="built_in">document</span>.getElementById(<span class="string">'input'</span>)</span></span><br><span class="line"><span class="javascript">    <span class="keyword">var</span> obj = &#123;&#125;</span></span><br><span class="line"><span class="javascript">    <span class="built_in">Object</span>.defineProperty(obj,<span class="string">'input'</span>,&#123;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">get</span>: function()&#123;</span></span><br><span class="line"><span class="undefined">            </span></span><br><span class="line"><span class="undefined">        &#125;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">set</span>: function(val)&#123;</span></span><br><span class="line"><span class="javascript">			<span class="built_in">document</span>.getElementById(<span class="string">'model-box'</span>).innerHTML = val        </span></span><br><span class="line"><span class="undefined">    	&#125;</span></span><br><span class="line"><span class="undefined">    &#125;)</span></span><br><span class="line"><span class="javascript">    input.addEventListener(<span class="string">'keyup'</span>,<span class="function"><span class="keyword">function</span>(<span class="params">e</span>)</span>&#123;</span></span><br><span class="line"><span class="undefined">        obj.input = e.target.value</span></span><br><span class="line"><span class="undefined">    &#125;)</span></span><br><span class="line"><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<h3 id="1-2定义多个属性"><a href="#1-2定义多个属性" class="headerlink" title="1.2定义多个属性"></a>1.2定义多个属性</h3><ul>
<li><p><code>Object.defineProperties()</code></p>
<blockquote>
<p>接收两个参数，第一个参数是目标对象，第二个参数是属性对象</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = &#123;&#125;</span><br><span class="line"><span class="built_in">Object</span>.defineProperties(obj,&#123;</span><br><span class="line">    name: &#123;</span><br><span class="line">        value: <span class="string">'jack'</span></span><br><span class="line">    &#125;,</span><br><span class="line">    age: &#123;</span><br><span class="line">        value: <span class="number">20</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
</li>
<li><p><code>Object.deinePropertyDescriptor()</code></p>
<blockquote>
<p>接收两个参数，目标对象和读取其描述符的属性名称 ，返回一个对象，取得指定属性的描述符</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = &#123;&#125;</span><br><span class="line"><span class="built_in">Object</span>.defineProperties(obj,&#123;</span><br><span class="line">    name: &#123;</span><br><span class="line">        value: <span class="string">'jack'</span></span><br><span class="line">    &#125;,</span><br><span class="line">    age: &#123;</span><br><span class="line">        value: <span class="number">20</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> descriptor = <span class="built_in">Object</span>.definePropertyDescriptor(obj,<span class="string">'name'</span>)</span><br><span class="line"><span class="built_in">console</span>.log(descriptor.value) <span class="comment">// "jack"</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<h3 id="2-1创建对象"><a href="#2-1创建对象" class="headerlink" title="2.1创建对象"></a>2.1创建对象</h3><h4 id="2-1-1工厂模式"><a href="#2-1-1工厂模式" class="headerlink" title="2.1.1工厂模式"></a>2.1.1工厂模式</h4><blockquote>
<p>抽象了创建具体对象的过程</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">createPerson</span>(<span class="params">name, age, job</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> o = <span class="keyword">new</span> <span class="built_in">Object</span>()</span><br><span class="line">    o.name = name</span><br><span class="line">    o.age = age</span><br><span class="line">    o.job = job</span><br><span class="line">    o.sayName = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        alert(<span class="keyword">this</span>.name)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> o</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> person1 = createPerson(<span class="string">'jack'</span>,<span class="number">20</span>,<span class="string">'doctor'</span>)</span><br></pre></td></tr></table></figure>
<h4 id="2-1-2构造函数模式"><a href="#2-1-2构造函数模式" class="headerlink" title="2.1.2构造函数模式"></a>2.1.2构造函数模式</h4><blockquote>
<p>用构造函数创建特定类型的对象</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params">name,age,job</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.name = name</span><br><span class="line">    <span class="keyword">this</span>.age = age</span><br><span class="line">    <span class="keyword">this</span>.job = job</span><br><span class="line">    <span class="keyword">this</span>.sayName = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        alert(<span class="keyword">this</span>.name)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> person1 = <span class="keyword">new</span> Person(<span class="string">'jack'</span>,<span class="number">20</span>,<span class="string">'Doctor'</span>)</span><br><span class="line"><span class="keyword">var</span> person2 = <span class="keyword">new</span> Person(<span class="string">'Mike'</span>,<span class="number">21</span>,<span class="string">'java'</span>)</span><br><span class="line"><span class="comment">// 两个实例的constructor属性均指向Person</span></span><br><span class="line"><span class="built_in">console</span>.log(person1.constructor == person2.constructor == Person)</span><br><span class="line"></span><br><span class="line">alert(person1 <span class="keyword">instanceof</span> <span class="built_in">Object</span>) <span class="comment">//true</span></span><br><span class="line">alert(person1 <span class="keyword">instanceof</span> Person) <span class="comment">//true</span></span><br><span class="line">alert(person2 <span class="keyword">instanceof</span> <span class="built_in">Object</span>) <span class="comment">//true</span></span><br><span class="line">alert(person2 <span class="keyword">instanceof</span> Person) <span class="comment">//true</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>与工厂模式不同之处</strong></p>
<ul>
<li>没有显式的创建对象</li>
<li>没有将属性和方法赋给this对象</li>
<li>没有 return 语句</li>
</ul>
</blockquote>
<blockquote>
<p><strong>创建构造函数实例（new 一个实例）要经历的4件事</strong></p>
<ol>
<li>创建一个新对象</li>
<li>将构造函数的作用域赋给新对象（因此this就指向了这个新对象）</li>
<li>执行构造函数中的代码（为新对象添加属性）</li>
<li>返回新对象</li>
</ol>
</blockquote>
<h4 id="2-1-3原型模式"><a href="#2-1-3原型模式" class="headerlink" title="2.1.3原型模式"></a>2.1.3原型模式</h4><blockquote>
<p>我们创建的每一个函数都有一个 <code>prototype</code>属性，这个属性是一个指针，指向一个对象，这个对象的用途就是包含可以由特定类型的实例共享的属性及方法</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params"></span>)</span>&#123;&#125;</span><br><span class="line"></span><br><span class="line">Person.prototype.name = <span class="string">'jack'</span></span><br><span class="line">Person.prototype.age = <span class="number">20</span></span><br><span class="line">Person.prototype.job = <span class="string">"Doctor"</span></span><br><span class="line">Person.prototype.sayName = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    alert(<span class="keyword">this</span>.name)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> person1 = <span class="keyword">new</span> Person()</span><br><span class="line">alert(person1.name) <span class="comment">// "jack"</span></span><br><span class="line"><span class="keyword">var</span> person2 = <span class="keyword">new</span> Person()</span><br><span class="line">alert(person2.name) <span class="comment">// "jack"</span></span><br></pre></td></tr></table></figure>
<p><img src="../../../../imgs/1571382645865.png" alt="1571382645865"></p>
<blockquote>
<p>构造函数的实例对象的<code>__proto__</code>指向了构造函数的<code>prototype</code>的构造器，而构造函数的<code>prototype</code>的构造器又指回构造函数本身</p>
</blockquote>
<h4 id="2-1-4-原型链"><a href="#2-1-4-原型链" class="headerlink" title="2.1.4 原型链"></a>2.1.4 原型链</h4><blockquote>
<p>当我们调用实例对象的属性或方法时，首先会在构造函数上寻找，若没有的话又会去构造函数的<code>prototype</code>上寻找，若还是没有就会在构造函数的<code>prototype</code>的<code>__proto__</code>的原型上寻找，这样一层一层向上寻找就会构成一个链</p>
<p>原型链是一种关系，实例对象和原型对象间的关系，通过原型<code>__proto__</code>来联系</p>
</blockquote>
<h4 id="2-1-5实例对象or原型对象"><a href="#2-1-5实例对象or原型对象" class="headerlink" title="2.1.5实例对象or原型对象"></a>2.1.5实例对象or原型对象</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params"></span>) </span>&#123;&#125;</span><br><span class="line"></span><br><span class="line">Person.prototype.name = <span class="string">"jack"</span></span><br><span class="line">Person.prototype.age = <span class="number">20</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> person1 = <span class="keyword">new</span> Person()</span><br><span class="line"><span class="keyword">var</span> person2 = <span class="keyword">new</span> Person()</span><br><span class="line"></span><br><span class="line">person1.name = <span class="string">"Mike"</span></span><br><span class="line"></span><br><span class="line">alert(person1.name) <span class="comment">// "Mike"----&gt;来自实例</span></span><br><span class="line">alert(person2.name) <span class="comment">// "jack"----&gt;来自原型</span></span><br></pre></td></tr></table></figure>
<ul>
<li><code>delete</code>操作符可以删除实例属性</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">delete</span> person1.name</span><br><span class="line">alert(person1.name) <span class="comment">// "jack"</span></span><br></pre></td></tr></table></figure>
<ul>
<li><code>hasOwnProperty()</code>用于检测一个属性是否属于实例,不存在返回false</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">person1.name = <span class="string">"Mike"</span></span><br><span class="line">alert(person1.hasOwnProperty(<span class="string">'name'</span>)) <span class="comment">// true</span></span><br><span class="line"><span class="keyword">delete</span> person1.name</span><br><span class="line">alert(person1.hasOwnProperty(<span class="string">'name'</span>)) <span class="comment">// false</span></span><br></pre></td></tr></table></figure>
<h4 id="2-1-6-in操作符"><a href="#2-1-6-in操作符" class="headerlink" title="2.1.6 in操作符"></a>2.1.6 <code>in</code>操作符</h4><blockquote>
<p>对于原型和实例属性，只要存在都会返回 <code>true</code></p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">person1.name = <span class="string">"Mike"</span></span><br><span class="line">alert(person1.name) <span class="comment">// "Mike"</span></span><br><span class="line">alert(name <span class="keyword">in</span> person1) <span class="comment">// true</span></span><br><span class="line"><span class="keyword">delete</span> person1.name</span><br><span class="line">alert(person1.name) <span class="comment">// "jack"</span></span><br><span class="line">alert(name <span class="keyword">in</span> person1) <span class="comment">// true</span></span><br></pre></td></tr></table></figure>
<p>所以可以通过<code>hasOwnProperty()</code>和<code>in</code>来确定该属性是实例属性还是原型属性</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">hasPrototypeProperty</span>(<span class="params">object,name</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> !object.hasOwnProperty(name) &amp;&amp; (name <span class="keyword">in</span> object)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="2-1-7Object-key-和Object-getOwnPropertyNames"><a href="#2-1-7Object-key-和Object-getOwnPropertyNames" class="headerlink" title="2.1.7Object.key()和Object.getOwnPropertyNames()"></a>2.1.7<code>Object.key()</code>和<code>Object.getOwnPropertyNames()</code></h4><ul>
<li><code>Object.key()</code>接收一个对象，以数组的形式返回该对象所有可枚举属性</li>
<li><code>Object.getOwnPropertyNames()</code> 返回所有属性（包括不可枚举）</li>
</ul>
<h4 id="2-1-8-原型对象的重写"><a href="#2-1-8-原型对象的重写" class="headerlink" title="2.1.8 原型对象的重写"></a>2.1.8 原型对象的重写</h4><blockquote>
<p>向上面那样一条一条添加原型属性难免有点麻烦，所以更常见的做法是用一个对象来添加所有的原型方法</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params"></span>)</span>&#123;&#125;</span><br><span class="line"></span><br><span class="line">Person.prototype = &#123;</span><br><span class="line">    name: <span class="string">"jack"</span>,</span><br><span class="line">    age: <span class="number">20</span>,</span><br><span class="line">    job: <span class="string">"Doctor"</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>这样写在方便的同时也会带来一个<strong>副作用</strong>，原本构造函数的<code>prototype</code>的<code>constructor</code>是指向他的构造函数的（这里是Person）,但是一旦重写了原型对象，他的<code>prototype</code>的<code>constructor</code>就不指向Person了，而是指向<strong><code>Object</code></strong></p>
</blockquote>
<p><strong>但是我们可以像下面的做法一样再把它设置回适当的值</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Person.prototype = &#123;</span><br><span class="line">    <span class="keyword">constructor</span>: Person, // 手动设置为 Person</span><br><span class="line">    name: "jack",</span><br><span class="line">    age: 20,</span><br><span class="line">    job: "Doctor"</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>但是这样写同样有问题，他会导致他的<code>constructor</code>属性的<code>Enumerable</code>属性变为true,(默认属性为false,是不可枚举的)</strong></p>
</blockquote>
<p><strong>解决方法</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 该方法必须兼容ECMAScript 5的引擎才行</span></span><br><span class="line"><span class="built_in">Object</span>.defineProperty(Person.prototype, <span class="string">"constructor"</span>,&#123;</span><br><span class="line">    enumerable: <span class="literal">false</span>,</span><br><span class="line">    value: Person</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h4 id="2-1-9原型的动态性"><a href="#2-1-9原型的动态性" class="headerlink" title="2.1.9原型的动态性"></a>2.1.9原型的动态性</h4><blockquote>
<p>由于在原型中查找值的过程是一次搜索，因此我们对原型对象所做的任何修改都可以在实例对象中得到反映，<strong>即使是先创建实例后修改原型</strong></p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params"></span>) </span>&#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> friend = <span class="keyword">new</span> Person()</span><br><span class="line"></span><br><span class="line">Person.prototype.name = <span class="string">"jack"</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(friend.name) <span class="comment">// "jack"</span></span><br></pre></td></tr></table></figure>
<p><strong>如果重写了原型对象呢？</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params"></span>) </span>&#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> friend = <span class="keyword">new</span> Person()</span><br><span class="line"></span><br><span class="line">Person.prototype = &#123;</span><br><span class="line">    <span class="keyword">constructor</span>: Person, </span><br><span class="line">    name: "jack",</span><br><span class="line">    age: 20,</span><br><span class="line">    job: "Doctor"</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">console.log(friend.name) // error</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>解析：</strong></p>
<p><strong>由于重写原型后原型的<code>constructor</code>属性不再指向原构造函数本身，而是指向<code>Object</code>,所以重写原型会切断构造函数与最初原型之间的联系，所以实例对象并不能访问到重写的原型属性</strong></p>
</blockquote>
<h4 id="2-1-10寄生构造函数模式和稳妥构造函数模式"><a href="#2-1-10寄生构造函数模式和稳妥构造函数模式" class="headerlink" title="2.1.10寄生构造函数模式和稳妥构造函数模式"></a>2.1.10寄生构造函数模式和稳妥构造函数模式</h4><blockquote>
<p>后续再看</p>
</blockquote>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/JavaScript/" rel="tag"># JavaScript</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2019/10/15/一些引用类型/" rel="next" title="一些引用类型">
                <i class="fa fa-chevron-left"></i> 一些引用类型
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2019/10/18/js垃圾回收机制/" rel="prev" title="js垃圾回收机制">
                js垃圾回收机制 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">Zhang_lei</p>
              <p class="site-description motion-element" itemprop="description">爱吃零食的猫</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">43</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/blog/tags/index.html">
                  <span class="site-state-item-count">22</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/403NotFound" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.zhihu.com/people/ai-chi-bao-mi-hua-de-mao/activities" target="_blank" title="知乎">
                      
                        <i class="fa fa-fw fa-send"></i>知乎</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://gitee.com/ZhangLei_c" target="_blank" title="码云">
                      
                        <i class="fa fa-fw fa-github-alt"></i>码云</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://weibo.com/5205419998/profile?rightmod=1&wvr=6&mod=personinfo&is_all=1" target="_blank" title="weibo">
                      
                        <i class="fa fa-fw fa-weibo"></i>weibo</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#面向对象及原型"><span class="nav-number">1.</span> <span class="nav-text">面向对象及原型</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1属性类型"><span class="nav-number">1.1.</span> <span class="nav-text">1.1属性类型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2定义多个属性"><span class="nav-number">1.2.</span> <span class="nav-text">1.2定义多个属性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1创建对象"><span class="nav-number">1.3.</span> <span class="nav-text">2.1创建对象</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-1工厂模式"><span class="nav-number">1.3.1.</span> <span class="nav-text">2.1.1工厂模式</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-2构造函数模式"><span class="nav-number">1.3.2.</span> <span class="nav-text">2.1.2构造函数模式</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-3原型模式"><span class="nav-number">1.3.3.</span> <span class="nav-text">2.1.3原型模式</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-4-原型链"><span class="nav-number">1.3.4.</span> <span class="nav-text">2.1.4 原型链</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-5实例对象or原型对象"><span class="nav-number">1.3.5.</span> <span class="nav-text">2.1.5实例对象or原型对象</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-6-in操作符"><span class="nav-number">1.3.6.</span> <span class="nav-text">2.1.6 in操作符</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-7Object-key-和Object-getOwnPropertyNames"><span class="nav-number">1.3.7.</span> <span class="nav-text">2.1.7Object.key()和Object.getOwnPropertyNames()</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-8-原型对象的重写"><span class="nav-number">1.3.8.</span> <span class="nav-text">2.1.8 原型对象的重写</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-9原型的动态性"><span class="nav-number">1.3.9.</span> <span class="nav-text">2.1.9原型的动态性</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-10寄生构造函数模式和稳妥构造函数模式"><span class="nav-number">1.3.10.</span> <span class="nav-text">2.1.10寄生构造函数模式和稳妥构造函数模式</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Zhang_lei</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>


<div>
<span id="busuanzi_container_site_pv">
  本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
<span id="busuanzi_container_site_uv">
    有<span id="busuanzi_value_site_uv"></span>人看过我的博客啦
</span>
</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
